﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace frmCheckStock
{
    public partial class frmIssueAdjustmentVoucher : Form
    {
        LogicUniversityT2Entities4 logic = new LogicUniversityT2Entities4();
        
        public frmIssueAdjustmentVoucher()
        {
            InitializeComponent();
        }

        private void frmIssueAdjustmentVoucher_Load(object sender, EventArgs e)
        {
            refreshGridView();
            calculate();
        }

        private void refreshGridView()
        {
            var c = from adj in logic.AdjustmentVouchers
                    join item in logic.Items on adj.ItemID equals item.ItemID where adj.Status =="Pending"
                    select new
                    {
                        ItemCode = adj.ItemID,
                        ItemDescription = item.ItemDescription,
                        Quantity = adj.Quantity,
                        Reason = adj.Remarks,
                    };

            var adjust = c.ToList();
            gvIssueAdjustmentVoucher.DataSource = adjust;
        }

         private void calculate()
         {
            for (int i = 0; i < gvIssueAdjustmentVoucher.Rows.Count;i++)
            {
                
                
                DataGridViewTextBoxCell itemID = (DataGridViewTextBoxCell)gvIssueAdjustmentVoucher.Rows[i].Cells["ItemCode"];
                string iID = itemID.Value.ToString();
                System.Nullable<Decimal> AverageUnitPrice =
             Convert.ToDecimal((from itemDetail in logic.ItemDetails
                                where itemDetail.ItemID == iID
                                select itemDetail.Price).Average());

               double avg = Convert.ToDouble(AverageUnitPrice);
               gvIssueAdjustmentVoucher.Rows[i].Cells["Price"].Value= avg;
               

               DataGridViewTextBoxCell itemQty = (DataGridViewTextBoxCell)gvIssueAdjustmentVoucher.Rows[i].Cells["Quantity"];
                int iQty = Convert.ToInt32(itemQty.Value);
                gvIssueAdjustmentVoucher.Rows[i].Cells["Amount"].Value = avg * iQty;
               
            }

         }

         private void btnIssueVoucher_Click(object sender, EventArgs e)
         {
             for (int i = 0; i < gvIssueAdjustmentVoucher.Rows.Count; i++)
             {
                 DataGridViewCheckBoxCell check = new DataGridViewCheckBoxCell();
                 check = (DataGridViewCheckBoxCell)gvIssueAdjustmentVoucher.Rows[i].Cells[0];
                 if (check.Value == null)
                     check.Value = false;
                 if (check.Value.ToString() == "")
                 {
                     check.Value = true;
                 }
                 if (Convert.ToBoolean(check.Value) == true)
                 {
                     DataGridViewTextBoxCell itemID = (DataGridViewTextBoxCell)gvIssueAdjustmentVoucher.Rows[i].Cells["ItemCode"];
                     string iID = itemID.Value.ToString();
                     AdjustmentVoucher iAV = logic.AdjustmentVouchers.First(isu => isu.ItemID == iID);
                     iAV.Status = "Approve";
                     logic.SaveChanges();
                     refreshGridView();
                     calculate();
                    
                     MessageBox.Show("Issued");
                 }
             }

         }




        }

        //private double calculateAveragePrice()
        //{
        //    double avg = 0;


        //    System.Nullable<Decimal> AverageUnitPrice =
        //     Convert.ToDecimal((from itemDetail in logic.ItemDetails
        //                        where itemDetail.ItemID == selectItemID
        //                        select itemDetail.Price).Average());

        //    avgPrice = Convert.ToDouble(AverageUnitPrice);



        //    return avgPrice;
        //    return avg;
        //    }
        

    }
        

